package com.telecom.at.json;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONObject;

import com.telecom.at.db.dac.Dac;
import com.telecom.at.db.model.BWList;
import com.telecom.at.db.model.SigBasAAA;
import com.telecom.at.db.model.User;

@WebServlet("/json/JsonSumSigBasAAA")
public class JsonSumSigBasAAA extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        int date = Integer.parseInt(request.getParameter("date"));
        int date2 = Integer.parseInt(request.getParameter("date"));
        //System.out.println(date);
        List<BWList>  bWList =Dac.getInstance().allBWList();
        Set<String> accountList=new HashSet<String>();
	       for(BWList bWList2:bWList)
	    	   accountList.add(bWList2.getAccount());
        List<SigBasAAA> sumSigBasAAAList = null;
        User curUser = (User) request.getSession().getAttribute("curUser");
        if (curUser.getId() == 0) {
        	
        	
            sumSigBasAAAList = Dac.getInstance().allSumSigBasAAA(date, date2);
        } else {
            sumSigBasAAAList = Dac.getInstance().sumSigBasAAA(curUser.getCity(), date, curUser.getCity(),
                    curUser.getCity(), date2);
        }
        List<SigBasAAA> flowList2 =new ArrayList<SigBasAAA>();
        for(SigBasAAA sigBasAAA:sumSigBasAAAList) {
			if(!accountList.contains(sigBasAAA.getAccount())&&flowList2.size()<10) {
				
				flowList2.add(sigBasAAA);
			}
		}
        
        JSONArray jsonArray = new JSONArray(flowList2);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("total", flowList2.size());
        jsonObject.put("subjects", jsonArray);
        // System.out.println(jsonArray);
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        // System.out.println(jsonObject.toString());
        response.getWriter().write(jsonObject.toString());
    }
}
